package cn.net.shuxing.yifu.common.mapper;

import cn.net.shuxing.yifu.common.bean.UserDealHistory;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

public interface UserDealHistoryMapper extends Mapper<UserDealHistory> {

    @Select("SELECT\n" +
            "SUM(deal_alive_coin_count)\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +
            "AND (deal_type = #{in} OR deal_type = #{out})\n" +
            "AND (deleted = 0)" +
            "AND type =  #{type}")
    public BigDecimal getTotalTransferCount(@Param("userId") Integer userId, @Param("in") int in, @Param("out") int out, @Param("type") Integer type);


    @Select("SELECT\n" +
            "SUM(deal_alive_coin_count)\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +
            "AND (deal_type = #{in} OR deal_type = #{out})\n" +
            "AND (deleted = 0)" +
            "AND type = #{type} " +
            "AND create_time > #{localDateTime}")
    public BigDecimal getMonthTransferCount(@Param("userId") Integer userId, @Param("in") int in, @Param("out") int out, @Param("type") Integer type, @Param("localDateTime") LocalDateTime localDateTime);

    @Select("SELECT\n" +
            "SUM(deal_alive_coin_count)\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
            "AND (deal_type = #{hangbuy} OR deal_type = #{hangsale} OR deal_type = #{sysbuyback}OR deal_type = #{fee})\n" +
            "AND (deleted = 0)" +
            "AND type =  #{type}")
    public BigDecimal getTotalDealCount(@Param("userId") Integer userId,
                                        @Param("hangbuy") int hangbuy, @Param("hangsale") int hangsale,
                                        @Param("sysbuyback") int sysbuyback, @Param("fee") int fee,
                                        @Param("type") Integer type);


    @Select("SELECT\n" +
            "SUM(deal_alive_coin_count)\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +
            "AND (deal_type = #{hangbuy} OR deal_type = #{hangsale} OR deal_type = #{sys} OR deal_type = #{fee})\n" +
            "AND (deleted = 0)" +
            "AND type = #{type} " +
            "AND create_time > #{localDateTime}")
    public BigDecimal getMonthDealCount(@Param("userId") Integer userId,
                                        @Param("hangbuy") Integer hangbuy,
                                        @Param("hangsale") Integer hangsale,
                                        @Param("sys") Integer sys,
                                        @Param("fee") Integer fee,
                                        @Param("localDateTime") LocalDateTime localDateTime);

    @Select("SELECT\n" +
            "*\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +SYSTEM_BUY_BACK
            "AND (deal_type = #{hangSale} OR deal_type = #{hangBuy} OR deal_type = #{cansel} OR deal_type = #{buyBack} OR deal_type = #{fee})\n" +
            "AND (deleted = 0)" +
            "AND type = #{type} " +
            "AND create_time BETWEEN #{startDate} And #{endDate}" +
            "ORDER BY create_time DESC")
    public List<UserDealHistory> getHangBuyAndHangSaleAndSystemBuyBackByUserIdAndDate(@Param("userId") Integer userId,
                                                                                      @Param("cansel") int cansel,
                                                                                      @Param("hangSale") int hangSale,
                                                                                      @Param("hangBuy") int hangBuy,
                                                                                      @Param("buyBack") int buyBack,
                                                                                      @Param("fee") int fee,
                                                                                      @Param("type") Integer type,
                                                                                      @Param("startDate") LocalDate startDate,
                                                                                      @Param("endDate") LocalDate endDate);

    @Select("SELECT\n" +
            "*\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +SYSTEM_BUY_BACK
            "AND (deal_type = #{dealType})\n" +
            "AND (deleted = 0)" +
            "AND type = #{type} " +
            "AND create_time BETWEEN #{startDate} And #{endDate}" +
            "ORDER BY create_time DESC")
    public List<UserDealHistory> getsByDateAndDealType(@Param("userId") Integer userId,
                                                       @Param("dealType") Integer dealType,
                                                       @Param("type") Integer type,
                                                       @Param("startDate") LocalDate startDate,
                                                       @Param("endDate") LocalDate endDate);

    @Select("SELECT\n" +
            "*\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +SYSTEM_BUY_BACK
            "AND (deal_type = 5)\n" +
            "AND (deal_scene =  #{dealScene})\n" +
            "AND (deleted = 0)" +
            "AND type = #{type} " +
            "AND create_time BETWEEN #{startDate} And #{endDate}" +
            "ORDER BY create_time DESC")
    public List<UserDealHistory> getsByUserIdAndDealSceneAndIsFee(@Param("userId") Integer userId,
                                                       @Param("dealScene") Integer dealScene,
                                                       @Param("type") Integer type,
                                                       @Param("startDate") LocalDate startDate,
                                                       @Param("endDate") LocalDate endDate);

    @Select("SELECT\n" +
            "*\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +SYSTEM_BUY_BACK
//            "AND (deal_type = 5)\n" +
            "AND (deal_scene =  #{dealScene})\n" +
            "AND (deleted = 0)" +
            "AND type = #{type} " +
            "AND create_time BETWEEN #{startDate} And #{endDate}" +
            "ORDER BY create_time DESC")
    public List<UserDealHistory> getAllHistory(@Param("userId") Integer userId,
                                                                  @Param("dealScene") Integer dealScene,
                                                                  @Param("type") Integer type,
                                                                  @Param("startDate") LocalDate startDate,
                                                                  @Param("endDate") LocalDate endDate);
    @Select("SELECT * FROM" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +SYSTEM_BUY_BACK
//            "AND (deal_type = 5)\n" +
//            "AND (deal_scene =  #{dealScene})\n" +
            "AND (deleted = 0)" +
            "AND (type = 0 or type =1) " +
            "AND create_time BETWEEN #{startDate} And #{endDate}" +
            "ORDER BY create_time DESC")
    public List<UserDealHistory> getHistoryNoType(@Param("userId") Integer userId,
//                                               @Param("dealScene") Integer dealScene,
                                               @Param("type") Integer type,
                                               @Param("startDate") LocalDate startDate,
                                               @Param("endDate") LocalDate endDate);
    @Select("SELECT\n" +
            "*\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +SYSTEM_BUY_BACK
            "AND (deal_scene = #{dealScene})\n" +
            "AND (deleted = 0)" +
            "AND type = #{type} " +
            "AND create_time BETWEEN #{startDate} And #{endDate}" +
            "ORDER BY create_time DESC")
    public List<UserDealHistory> getsByDateAndDealScene(@Param("userId") Integer userId,
                                                        @Param("dealScene") Integer dealScene,
                                                        @Param("type") Integer type,
                                                        @Param("startDate") LocalDate startDate,
                                                        @Param("endDate") LocalDate endDate);

    @Select("SELECT\n" +
            "*\n" +
            "FROM\n" +
            "\tuser_deal_history\n" +
            "WHERE\n" +
            "\t(user_id = #{userId})\n" +
//            "AND (user_id = 157)\n" +SYSTEM_BUY_BACK
            "AND (deal_type = #{in} OR deal_type = #{out})\n" +
            "AND (deleted = 0)" +
            "AND type = #{type} " +
            "AND create_time BETWEEN #{startDate} And #{endDate}" +
            "ORDER BY create_time DESC")
    public List<UserDealHistory> getTransferOutAndTransferInByUserIdAndDate(@Param("userId") Integer userId,
                                                                            @Param("in") int in,
                                                                            @Param("out") int out,
                                                                            @Param("type") Integer type,
                                                                            @Param("startDate") LocalDate startDate,
                                                                            @Param("endDate") LocalDate endDate);
}
